package com.stroke.tools.animation;

import com.stroke.util.AnimationHelper;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;

/**
 * Author: Igor Bubelov
 * Date: 3/8/11 5:37 PM
 */
public class MainScreenController extends Controller {
    private FrameList frameList;
    private JLabel imageLabel;

    private JButton moveUpButton;
    private JButton moveDownButton;

    private JButton addFrameButton;
    private JButton removeFrameButton;

    private JButton loadButton;
    private JButton saveButton;
    private JButton exitButton;

    public MainScreenController(Screen frame) {
        super(frame);

        initUI();

        initActions();
    }

    private void initUI() {
        frameList = (FrameList) getScreen().getComponent("FrameList");
        imageLabel = (JLabel) getScreen().getComponent("ImageLabel");
        moveUpButton = (JButton) getScreen().getComponent("MoveUpButton");
        moveDownButton = (JButton) getScreen().getComponent("MoveDownButton");
        addFrameButton = (JButton) getScreen().getComponent("AddFrameButton");
        removeFrameButton = (JButton) getScreen().getComponent("RemoveFrameButton");
        loadButton = (JButton) getScreen().getComponent("LoadButton");
        saveButton = (JButton) getScreen().getComponent("SaveButton");
        exitButton = (JButton) getScreen().getComponent("ExitButton");
    }

    private void initActions() {
        initFrameListAction();

        initMoveUpButtonAction();

        initMoveDownButtonAction();

        initAddFrameButtonAction();

        initRemoveFrameButtonAction();

        initLoadButtonAction();

        initSaveButtonAction();

        initExitButtonAction();
    }

    private void initFrameListAction() {
        frameList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (frameList.getSelectedIndex() != -1) {
                    Frame frame = (Frame) frameList.getSelectedValue();
                    ImageIcon icon = new ImageIcon(frame.getImage());
                    imageLabel.setIcon(icon);
                    frameList.requestFocus();
                } else {
                    imageLabel.setIcon(null);
                }
            }
        });
    }

    private void initMoveUpButtonAction() {
        moveUpButton.setAction(new AbstractAction("↑") {
            public void actionPerformed(ActionEvent e) {
                frameList.moveFrame(frameList.getSelectedIndex(), 1);
            }
        });
    }

    private void initMoveDownButtonAction() {
        moveDownButton.setAction(new AbstractAction("↓") {
            public void actionPerformed(ActionEvent e) {
                frameList.moveFrame(frameList.getSelectedIndex(), -1);
            }
        });
    }

    private void initAddFrameButtonAction() {
        addFrameButton.setAction(new AbstractAction("+") {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();

                fileChooser.setMultiSelectionEnabled(true);

                if (fileChooser.showOpenDialog((JFrame) getScreen()) == JFileChooser.APPROVE_OPTION) {
                    for (int i = 0; i < fileChooser.getSelectedFiles().length; i++) {
                        File current = fileChooser.getSelectedFiles()[i];

                        if (current.isDirectory()) {
                            return;
                        }

                        int position = frameList.getModel().getSize() + 1;

                        frameList.addFrame(new Frame(current, position));
                    }
                }
            }
        });
    }

    private void initRemoveFrameButtonAction() {
        removeFrameButton.setAction(new AbstractAction("-") {
            public void actionPerformed(ActionEvent e) {
                if (frameList.getSelectedIndex() != -1) {
                    int index = frameList.getSelectedIndex();

                    frameList.removeFrame((Frame) frameList.getSelectedValue());

                    index = index > 0
                            ? index - 1
                            : index < frameList.getModel().getSize() - 1
                                ? index + 1
                                : -1;

                    frameList.setSelectedIndex(index);

                    frameList.getListSelectionListeners()[0].valueChanged(null);
                }
            }
        });
    }

    private void initLoadButtonAction() {
        final JFileChooser chooser = new JFileChooser();

        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

        loadButton.setAction(new AbstractAction("Load") {
            public void actionPerformed(ActionEvent e) {
                chooser.showOpenDialog((JFrame) getScreen());

                AnimationFile animation = new AnimationFile(chooser.getSelectedFile());

                animation.open();

                frameList.setData(animation.getFramesSorted());

                animation.close();
            }
        });
    }

    private void initSaveButtonAction() {
        final JFileChooser chooser = new JFileChooser();

        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

        saveButton.setAction(new AbstractAction("Save") {
            public void actionPerformed(ActionEvent e) {
                if (frameList.getModel().getSize() == 0) {
                    return;
                }

                if (chooser.showSaveDialog((JFrame) getScreen()) == JFileChooser.APPROVE_OPTION) {
                    File selected = chooser.getSelectedFile();

                    if (!selected.getName().split("\\.")[selected.getName().split("\\.").length - 1].equals("sta")) {
                        selected = new File(selected.getAbsolutePath() + ".sta");
                    }

                    AnimationHelper.saveAnimation(selected, frameList.getModel().getElements());
                }
            }
        });
    }

    private void initExitButtonAction() {
        exitButton.setAction(new AbstractAction("Exit") {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }
}
